What is lodash.forown?
The lodash.forown package is a utility function from the Lodash library that iterates over own enumerable string keyed properties of an object and invokes a function for each property. This is useful for object manipulation and iteration tasks.
What are lodash.forown's main functionalities?
Iterate over object properties
This feature allows you to iterate over the own enumerable string keyed properties of an object. The provided function is invoked for each property, receiving the value and key as arguments.
const _ = require('lodash.forown');
const object = { 'a': 1, 'b': 2, 'c': 3 };
_.forOwn(object, function(value, key) {
console.log(key, value);
});
Early exit from iteration
This feature allows you to exit the iteration early by returning `false` from the iteratee function. This can be useful when you need to stop processing once a certain condition is met.
const _ = require('lodash.forown');
const object = { 'a': 1, 'b': 2, 'c': 3 };
_.forOwn(object, function(value, key) {
if (key === 'b') {
return false;
}
console.log(key, value);
});
Other packages similar to lodash.forown
object-foreach
The object-foreach package provides similar functionality to lodash.forown by iterating over the own properties of an object. It is a lightweight alternative but lacks the extensive utility functions provided by Lodash.
lodash.forown v4.4.0
The lodash method _.forOwn
exported as a Node.js module.
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.forown
In Node.js:
var forOwn = require('lodash.forown');
See the documentation or package source for more details.